package com.wh.switchbtn;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Created by E E E
 * Data : 2017/3/22 0022.
 */

public class SwitchBtn extends LinearLayout {

    private RectF mThumbRect;
    private RectF mBgRect;

    private Paint mPaint;
    private Paint mBgPaint;

    private float mHeight;

    private float mWidth;

    private float radius;

    private boolean isOpen = false;

    private float mInterval = 10;


    public SwitchBtn(Context context) {
        this(context, null);
    }

    public SwitchBtn(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SwitchBtn(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mHeight = dip2px(context, 40);
        mWidth = dip2px(context, 80);

        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.unOpenColor));
        mPaint.setAntiAlias(true);
        mBgPaint = new Paint();
        mBgPaint.setColor(getResources().getColor(R.color.bgColor));
        mBgPaint.setAntiAlias(true);


        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) mWidth, (int) mHeight);
        layoutParams.gravity = Gravity.CENTER;
        setLayoutParams(layoutParams);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            setBackground(getResources().getDrawable(R.drawable.bg_switch_btn));
        } else {
            setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_switch_btn));
        }
        btnX = mHeight / 2;
        btnY = mHeight / 2;
        radius = mHeight / 2;
        mThumbRect = new RectF();
        mBgRect = new RectF();
        mBgRect.set(0, mInterval, mWidth, mHeight - mInterval);
        mThumbRect.set(0, 0, mHeight, mHeight);

    }


    public void setBtnBackgroundColor(int color) {

    }

    public void setBtnColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(mBgRect, mHeight - mInterval * 2, mHeight - mInterval * 2, mBgPaint);
        canvas.drawRoundRect(mThumbRect, radius, radius, mPaint);
//        canvas.drawCircle(btnX, btnY, radius, mPaint);
    }

    float btnX, btnY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
//        if (x<0 || x>mWidth || y<0 || y>mHeight)
//            return super.onTouchEvent(event);
        float currentX = 0, currentY, lastX = 0, lastY;
        long downTime = 0;
        long upTime = 0;

        Log.e("onTouchEvent", " btnX : " + btnX + " X : " + x);

        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (radius <= Math.abs(x - btnX))
                    return super.onTouchEvent(event);
                lastX = x;
                lastY = y;
                mPaint.setColor(getResources().getColor(R.color.pressColor));
                downTime = System.currentTimeMillis();

                Log.e("ACTION_DOWN", " X : " + btnX);
                return true;
            case MotionEvent.ACTION_MOVE:
//                if (radius <= Math.round(x - btnX))
//                    return super.onTouchEvent(event);

                if (x < mHeight / 2) {
                    btnX = mHeight / 2;
                } else if (x > (mWidth - mHeight / 2)) {
                    btnX = mWidth - mHeight / 2;
                    Log.e("btnX > ", " btnX : " + btnX);
                } else {
                    Log.e("else", " btnX : " + btnX + " X : " + x);
                    btnX = x;
                }

                setThumbRect(btnX);

                Log.e("ACTION_MOVE", " btnX : " + btnX + " X : " + x);
                return true;
            case MotionEvent.ACTION_UP:
                if (btnX <= radius) {
                    btnX = radius;
                } else {
                    btnX = mWidth - radius;
                }
                upTime = System.currentTimeMillis();
                if (Math.abs(upTime- downTime) < 200){
                    setOnOff(btnX);
                }
//                if (Math.round(currentX - lastX) < 50) {
//                }
                if (isOpen) {
                    mPaint.setColor(getResources().getColor(R.color.openColor));
                } else {
                    mPaint.setColor(getResources().getColor(R.color.unOpenColor));
                }
                setThumbRect(btnX);
                Log.e("ACTION_UP", " X : " + btnX);

                return true;
        }
        return super.onTouchEvent(event);
    }

    private void setThumbRect(float x) {
        mThumbRect.set(x - radius, 0, x + radius, mHeight);

        invalidate();
    }

    private void setOnOff(float x) {
        if (isOpen) {
            this.btnX = radius;
            isOpen = false;
        } else {
            this.btnX = mWidth - radius;
            isOpen = true;
        }
//        invalidate();
    }

    public boolean getHasOpen(){
        return isOpen;
    }

    //dp --> px
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}







